package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Activity.CommunityActivity;
import com.example.city_home_app.Bean.Down_Community;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_Community extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<Down_Community> list;
    private ApiModel model = new ApiModel();
    private List<Integer> flag;

    public Adapter_Community(Context context) {
        this.context = context;
    }

    public void setList(List<Down_Community> list) {
        this.list = list;
        flag = new ArrayList<>();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_community_allcommunity, parent, false);
        Adapter_Community.ViewHolder viewHolder = new Adapter_Community.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final Down_Community bean = list.get(position);
        if (bean == null) {
            return;
        }
        final Adapter_Community.ViewHolder viewHolder = (Adapter_Community.ViewHolder) holder;
        viewHolder.tvRecitemCommunietAllcommunityFocuscount.setText(bean.getCount() + "");
        viewHolder.tvRecitemCommunityAllcommunityName.setText(bean.getCommunity_name());
        Picasso.with(context)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/" + bean.getCommunity_pic())
                .into(viewHolder.imgRecitemCommunityAllcommunity);


        RetrofitListener<Down_Universe> isfocusretrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    viewHolder.btnRecitemCommunityAllcommunityFocus.setText("取消关注");
                    flag.add(1);
                } else {
                    viewHolder.btnRecitemCommunityAllcommunityFocus.setText("关注");
                    flag.add(0);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };
        final RetrofitListener<Down_Universe> focusretrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    viewHolder.btnRecitemCommunityAllcommunityFocus.setText("取消关注");
                    flag.set(position, 0);
                    UIUtils.showToast(context, "关注成功", UIUtils.LENGTH_SHORT);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };
        final RetrofitListener<Down_Universe> unfocusretrofitListener = new RetrofitListener<Down_Universe>() {
            @Override
            public void onResponse(Down_Universe listener) {
                if (listener.getMessage().equals("success")) {
                    viewHolder.btnRecitemCommunityAllcommunityFocus.setText("关注");
                    flag.set(position, 0);
                    UIUtils.showToast(context, "取消成功", UIUtils.LENGTH_SHORT);
                }
            }

            @Override
            public void onFail(String msg) {

            }
        };

        model.isfocuscommunity(bean.getCommunity_id(), BaseActivity.getUserinfo().getUserid(), isfocusretrofitListener);


        viewHolder.btnRecitemCommunityAllcommunityFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (flag.get(position) == 1) {
                    viewHolder.tvRecitemCommunietAllcommunityFocuscount.setText("" + (bean.getCount() - 1));
                    model.unfocuscommunity(bean.getCommunity_id(), BaseActivity.getUserinfo().getUserid(), unfocusretrofitListener);
                } else {
                    viewHolder.tvRecitemCommunietAllcommunityFocuscount.setText("" + (bean.getCount() + 1));
                    model.focuscommunity(bean.getCommunity_id(), BaseActivity.getUserinfo().getUserid(), focusretrofitListener);
                }
            }
        });
        viewHolder.layoutRecitemCommunityall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, CommunityActivity.class);
                intent.putExtra("id",bean.getCommunity_id());
                intent.putExtra("name",bean.getCommunity_name());
                context.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_recitem_community_allcommunity)
        ImageView imgRecitemCommunityAllcommunity;
        @BindView(R.id.tv_recitem_community_allcommunity_name)
        TextView tvRecitemCommunityAllcommunityName;
        @BindView(R.id.imageView2)
        ImageView imageView2;
        @BindView(R.id.tv_recitem_communiet_allcommunity_focuscount)
        TextView tvRecitemCommunietAllcommunityFocuscount;
        @BindView(R.id.btn_recitem_community_allcommunity_focus)
        Button btnRecitemCommunityAllcommunityFocus;
        @BindView(R.id.layout_recitem_communityall)
        ConstraintLayout layoutRecitemCommunityall;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
